import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'package:time_manager/app/model/item_list_model.dart';
import 'package:time_manager/app/store/global_data.dart';
import 'package:time_manager/dict/table_dict.dart';
import 'package:time_manager/utils/DataBaseRecord.dart';
import 'package:time_manager/utils/timeDeal.dart';

class TimeLogistic extends StatelessWidget {
  TimeLogistic({Key? key}) : super(key: key);
  final GlobalData store = Get.find<GlobalData>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Obx(() => Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                TextButton(
                    onPressed: () async {
                      final datePick = await showDatePicker(
                          context: Get.context!,
                          firstDate: DateTime(2000),
                          lastDate: DateTime.parse('${store.endTime.value}'),
                          locale: const Locale('zh'),
                          initialDate:
                              DateTime.parse('${store.startTime.value}'));
                      if (datePick != null) {
                        var _dateTime = datePick;
                        var temp = TimeUtil().getLocalTime(
                            _dateTime.millisecondsSinceEpoch.toString(),
                            format: 'day');
                        store.changeStart(temp);
                      }
                    },
                    child: Text(
                      store.startTime.value,
                      style: const TextStyle(color: Colors.white, fontSize: 20),
                    )),
                const Text('至',
                    style: const TextStyle(color: Colors.white, fontSize: 18)),
                TextButton(
                    onPressed: () async {
                      final datePick = await showDatePicker(
                          context: Get.context!,
                          firstDate: DateTime.parse('${store.startTime.value}'),
                          lastDate: DateTime(2100),
                          locale: const Locale('zh'),
                          initialDate:
                              DateTime.parse('${store.endTime.value}'));
                      if (datePick != null) {
                        var _dateTime = datePick;
                        var temp = TimeUtil().getLocalTime(
                            _dateTime.millisecondsSinceEpoch.toString(),
                            format: 'day');
                        store.changeEnd(temp);
                      }
                    },
                    child: Text(store.endTime.value,
                        style:
                            const TextStyle(color: Colors.white, fontSize: 20)))
              ],
            )),
      ),
      body: Obx(() {
        return Column(
          children: [
            Container(
              margin: const EdgeInsets.symmetric(vertical: 10),
              child: DefaultTextStyle(
                style: const TextStyle(fontSize: 16, color: Colors.blue),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text('总计:${store.filterList.length}次'),
                    const SizedBox(
                      width: 20,
                    ),
                    Text('时长:${store.totalTime.toString()}分钟'),
                    const SizedBox(
                      width: 10,
                    ),
                    Text('${(store.totalTime / 60).toStringAsFixed(2)}小时'),
                  ],
                ),
              ),
            ),
            // ElevatedButton(
            //     onPressed: () async {

            //       // print(temp);
            //     },
            //     child: const Text('获取列表')),
            Expanded(
                child: store.filterList.isEmpty
                    ? Center(child: const Text('当前没有日程'))
                    : ListView.builder(
                        itemCount: store.filterList.length,
                        itemExtent: 70,
                        itemBuilder: (_, index) {
                          ItemList it = store.filterList[index];
                          return Container(
                            margin: const EdgeInsets.symmetric(
                                vertical: 4, horizontal: 8),
                            child: Card(
                              child: Padding(
                                padding: const EdgeInsets.all(16.0),
                                child: DefaultTextStyle(
                                  style: const TextStyle(
                                      color: Colors.black, fontSize: 16),
                                  child: Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: [
                                        Text(TimeUtil()
                                            .getLocalTime(it.time.toString())),
                                        Text(it.duration.toString() + '分钟')
                                      ]),
                                ),
                              ),
                            ),
                          );
                        }))
          ],
        );
        // return ListView.builder(itemBuilder: (_, index) {
        //   return Text(index.toString());
        // });
      }),
    );
  }
}

class TimeList extends StatefulWidget {
  final Function(dynamic item) confirm;
  const TimeList({Key? key, required this.confirm}) : super(key: key);

  @override
  State<TimeList> createState() => _TimeListState();
}

class _TimeListState extends State<TimeList> {
  final timeList = [
    {"name": "10分钟", "value": 10},
    {"name": "20分钟", "value": 20},
    {"name": "30分钟", "value": 30},
    {"name": "1小时", "value": 60},
    {"name": "1.5小时", "value": 90},
    {"name": "2小时", "value": 120},
    {"name": "2.5小时", "value": 150},
    {"name": "3小时", "value": 180},
  ];
  dynamic curretItem = {"name": "10分钟", "value": 10};
  @override
  Widget build(BuildContext context) {
    return Material(
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(children: [
          Wrap(
            children: timeList
                .map((it) => GestureDetector(
                      onTap: () {
                        setState(() {
                          curretItem = it;
                        });
                      },
                      child: Container(
                        padding: const EdgeInsets.symmetric(
                            horizontal: 10, vertical: 8),
                        margin: const EdgeInsets.only(right: 10, bottom: 10),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(10),
                          color: curretItem['value'] == it['value']
                              ? Theme.of(context).primaryColor
                              : Colors.grey[200],
                        ),
                        child: Text(
                          it['name'] as String,
                          style: TextStyle(
                              color: curretItem['value'] == it['value']
                                  ? Colors.white
                                  : Colors.black),
                        ),
                      ),
                    ))
                .toList(),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                  onPressed: () {
                    widget.confirm(curretItem);
                    Get.back();
                  },
                  child: const Text('确定')),
              const SizedBox(
                width: 20,
              ),
              TextButton(
                  onPressed: () {
                    Get.back();
                  },
                  child: const Text('取消'))
            ],
          )
        ]),
      ),
    );
  }
}
